const whiteList = ["/login","/captchaImage","/logout","/games/games/gameTitle/treeselect","/games/games/gameTitle/selectModuleList","/games/games/wx/findOpenId","/games/games/addscore","/games/games/getscorelist"];

module.exports = (options)=>{
  return async (ctx,next)=> {
    const token = ctx.request.header.authorization;
    const userinfo = ctx.session.userinfo;
    let decode = '';
    // if(ctx.request.method === 'GET') {
    //   await next();
    //   return;
    // }
    // console.log(ctx.request,88888);
    let bool = whiteList.find(el=>ctx.request.url.includes(el));
    if(bool){
      await next();
      return;
    }
    if(!userinfo){
      ctx.status = 200;
      ctx.body = {
        code:401,
        msg:'请重新登陆',
        data:null
      }
      return 
    }
    if(token){
      try {
        decode = ctx.app.jwt.verify(token,options.secret);
        await next(decode);
      } catch (error) {
        console.log(error,"errr")
        ctx.status = 200;
        ctx.body={
          code:401,
          msg:"登陆过期",
          data:null
        };
        return;
      }
    }else{
      ctx.status = 200;
      ctx.body = {
        code:401,
        msg:'缺少token',
        data:null
      }
      return;
    }
  }
}